from dataclasses import dataclass
from typing import Optional
from datetime import datetime

@dataclass
class Game:
    """游戏数据模型类"""
    name: str
    price: float
    discount: int
    rating: Optional[float] = None
    release_date: Optional[datetime] = None
    description: Optional[str] = None
    categories: Optional[list] = None
    
    def to_dict(self) -> dict:
        """转换为字典格式"""
        return {
            'name': self.name,
            'price': self.price,
            'discount': self.discount,
            'rating': self.rating,
            'release_date': self.release_date.isoformat() if self.release_date else None,
            'description': self.description,
            'categories': self.categories
        }
    
    @classmethod
    def from_dict(cls, data: dict) -> 'Game':
        """从字典创建游戏对象"""
        return cls(
            name=data.get('name', ''),
            price=data.get('price', 0.0),
            discount=data.get('discount', 0),
            rating=data.get('rating'),
            release_date=datetime.fromisoformat(data['release_date']) if data.get('release_date') else None,
            description=data.get('description'),
            categories=data.get('categories', [])
        ) 